package com.youbug.mall.coupon_sms.service.impl;

import com.youbug.mall.coupon_sms.entity.SmsSeckillSession;
import com.youbug.mall.coupon_sms.entity.SmsSeckillSkuRelation;
import com.youbug.mall.coupon_sms.mapper.SmsSeckillSessionMapper;
import com.youbug.mall.coupon_sms.service.ISmsSeckillSessionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youbug.mall.coupon_sms.service.ISmsSeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.youbug.mall.coupon_sms.entity.SmsSeckillSession;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 秒杀活动场次 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-07-20
 */
@Service
public class SmsSeckillSessionServiceImpl extends ServiceImpl<SmsSeckillSessionMapper, SmsSeckillSession> implements ISmsSeckillSessionService {

        @Autowired
        private ISmsSeckillSkuRelationService seckillSkuRelationService;

        @Override
        public IPage<SmsSeckillSession> getSmsSeckillSessionPage(QueryVo<SmsSeckillSession> queryVo) {

        Page<SmsSeckillSession> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<SmsSeckillSession> wrapper = new QueryWrapper<>();

        SmsSeckillSession smsSeckillSession = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(smsSeckillSession)){
            if(smsSeckillSession.getId()!=null){
            wrapper.eq("id", smsSeckillSession.getId());
            }
            if(StringUtils.hasText(smsSeckillSession.getName())){
                wrapper.eq("name", smsSeckillSession.getName());
            }
            if(!ObjectUtils.isEmpty(smsSeckillSession.getStartTime())){
                wrapper.gt("start_time", smsSeckillSession.getStartTime());
            }
            if(!ObjectUtils.isEmpty(smsSeckillSession.getEndTime())){
                wrapper.gt("end_time", smsSeckillSession.getEndTime());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<SmsSeckillSession> page = this.page(paramPage, wrapper);

        return page;
        }

    @Override
    public List<SmsSeckillSession> getLates3DaySession() {
        //计算最近三天
        //查出这三天参与秒杀活动的商品
        List<SmsSeckillSession> list = this.baseMapper.selectList(new QueryWrapper<SmsSeckillSession>()
                .between("start_time", startTime(), endTime()));

        if (list != null && list.size() > 0) {
            List<SmsSeckillSession> collect = list.stream().map(session -> {
                Long id = session.getId();
                //查出sms_seckill_sku_relation表中关联的skuId
                List<SmsSeckillSkuRelation> relationSkus = seckillSkuRelationService.list(new QueryWrapper<SmsSeckillSkuRelation>()
                        .eq("promotion_session_id", id));
                session.setRelationSkusTemp(relationSkus);
                return session;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }
    /**
     * 当前时间
     * @return
     */
    private String startTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);

        //格式化时间
        String startFormat = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return startFormat;
    }

    /**
     * 结束时间
     * @return
     */
    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(plus, max);

        //格式化时间
        String endFormat = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return endFormat;
    }
       /* @Transactional(transactionManager = "transactionManagerXXX", rollbackFor = Exception.class)
        public boolean saveBatch(Collection<SmsSeckillSession> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
        }
        */
}
